package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author by mycp
 * @Classname StrategyCatalogServiceImpl
 * @Description TODO
 * @Date 2018/11/8 11:05
 */
@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Override
    public PageInfo query(StrategyCatalogQueryObject qo) {
        // 每页查询 5 条数据
        qo.setPageSize(5);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        // 判断新增的时候是否有选择sequence, 如果没有, 自动加上
        if (strategyCatalog.getSequence() == null){
            // 用户没有选择序号, 自动加上序号
            // 先查询当前攻略的最大序号
            Integer sequence = strategyCatalogMapper
                    .selectSequenceByStategyId(strategyCatalog.getStrategyId());
            // 判断是否为null, 为null默认为 1, 不为null 则 +1 再设置进去 strategyCatalog 对象中
            if (sequence == null){
                sequence = 0;
            }
            strategyCatalog.setSequence(sequence + 1);
        }
        // 判断是更新还是保存操作
        if (strategyCatalog.getId() == null){
            strategyCatalogMapper.insert(strategyCatalog);
        } else {
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }
    }

    @Override
    public List<StrategyCatalog> selectEnable(Long strategyId) {
        return strategyCatalogMapper.getCatalogsByStrategyId(strategyId);
    }

}
